<?php
/**
* @author wolf <lang.feng@zol.com.cn>
* @copyright (c)
*/

class Libs_PopMsg {

	protected $msg = array();

	protected $timeout = 2;

	protected $backurl = '';

	protected $msgtype = 'failure';

	protected $msgControl = 'PopMsg';

	protected $ignore = false;
	protected $stop = false;

	public function __construct($msg = '', $msgtype = 'failure') {
		$this->addMsg($msg);
		$this->setMsgtype($msgtype);
	}

	public function addMsg($msg = '') {
		if (is_array($msg)) {
			$this->msg = array_merge($this->msg, $msg);
		} else {
			array_push($this->msg, $msg);
		}
		return true;
	}

	public function setTimeout($timeout = 3) {
		$this->timeout = $timeout;
		return true;
	}

	public function setBackurl($backurl = '') {
		$this->backurl = urlencode($backurl);
		return true;
	}

	public function setMsgtype($msgtype = 'failure') {
		$this->msgtype = $msgtype;
		return true;
	}

	public function setMsgControl($msgControl='PopMsg') {
		$this->msgControl = $msgControl;
		return true;
	}

	/**
	* 是否继续执行
	*/
	public function setIgnore($ignore = false) {
		$this->ignore = (bool)$ignore;
		return true;
	}


	/**
	* 是否不跳转
	*/
	public function setStop($stop = 0) {
		$this->stop = (int)$stop;
		return true;
	}

	public function run() {
		if ($this->msg && is_array($this->msg)) {
            $msg = '';
            foreach($this->msg as $m){
                $msg .= '&msg[]=' . urlencode($m);
            }
		} else {
			$msg = '&msg=' . urlencode($this->msg);
		}
		$url = 'index.php?c=' . $this->msgControl
				. $msg
				. '&type=' . $this->msgtype
				. '&backurl=' . $this->backurl
				. '&stop=' . $this->stop
				. '&timeout=' . $this->timeout;

		header('location:' . $url);

		$this->ignore || exit();
	}
	
	public function __destruct() {
		$this->run();
	}
}

